package com.japher.hackson;

import android.app.Application;
import android.os.Handler;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;

public class HacksonApplication extends Application{

 // 百度MapAPI的管理类
    public LocationClient mLocationClient = null;
    private Handler handler = new Handler();
    
    
    @Override
    public void onCreate() {
        
        super.onCreate();
        AppContext.getInstance().setContext(this);
        
        SDKInitializer.initialize(this);
        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        //mLocationClient.registerLocationListener( myListener );    //注册监听函数
    }
    
    private void setLocationOption()
    {
        if(mLocationClient!= null)
        {
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true);                                //打开gps
            option.setCoorType("bd09ll");
            option.setAddrType("all");
            option.setServiceName("com.baidu.location.service_v2.9");//设置坐标类型为bd09ll
            option.setPriority(LocationClientOption.NetWorkFirst);  //设置网络优先
            option.setScanSpan(5000);
            option.disableCache(true);
            mLocationClient.setLocOption(option);
        }
    }
    
    public void closeGps()
    {
        try
        {
            mLocationClient.stop();
        }
        catch(Exception e)
        {
            
        }
    }
    
    public void locate(BDLocationListener litener)
    {
        try
        {
            mLocationClient.stop();
            
            setLocationOption();
            mLocationClient.registerLocationListener(litener);
            mLocationClient.start();
            
            handler.postDelayed(new Runnable()
            {
                
                @Override
                public void run()
                {
                    closeGps();
                }
            }, 30000);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}
